package com.masterclient.app.modules.shop;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.shop.DealerShippingItemBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.adapter.DealerShippingListAdapter;
import com.masterclient.app.modules.shop.contract.DealerShippingListContract;
import com.masterclient.app.modules.shop.contract.DealerShippingListPresenter;
import com.masterclient.app.widget.TitlebarView;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/9/11.
 */
@Route(path = AroutePath.DEALER_SHIPPING_LIST_ACTIVITY)
public class DealerShippingListActivity extends BaseActivity<DealerShippingListPresenter> implements DealerShippingListContract.View {

    @BindView(R.id.dealer_shipping_list_titlebar)
    TitlebarView dealerShippingListTitlebar;
    @BindView(R.id.dealer_shipping_list_recycler)
    RecyclerView dealerShippingListRecycler;
    @BindView(R.id.dealer_shipping_list_refresh)
    SwipeRefreshLayout dealerShippingListRefresh;

    @Autowired(name = "product_id")
    String productId;
    @Autowired(name = "delivery_id")
    String deliveryId;

    private DealerShippingListAdapter dealerShippingAdapter;
    private int clickPos;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealer_shipping_list);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarDarkFont(true).init();

        dealerShippingAdapter = new DealerShippingListAdapter();
        dealerShippingListRecycler.setLayoutManager(new LinearLayoutManager(this));
        dealerShippingListRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = ScreenUtils.dp2px(10);
            }
        });
        dealerShippingListRecycler.setAdapter(dealerShippingAdapter);
        dealerShippingAdapter.bindToRecyclerView(dealerShippingListRecycler);

        refreshData();

        initListener();

    }

    private void refreshData(){
        mPresenter.getShippingList(deliveryId);
    }

    private void initListener() {

        dealerShippingListTitlebar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                ARouter.getInstance().build(AroutePath.DEALER_SHIPPING_MANAGER_ACTIVITY).navigation(DealerShippingListActivity.this,0x87);
            }
        });

        dealerShippingListRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });

        dealerShippingAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                DealerShippingItemBean bean = dealerShippingAdapter.getData().get(position);
                if ("1".equals(bean.getCheck_delivery())) return;
                clickPos = position;
                mPresenter.selectShipping(productId,bean.getDelivery_id());
            }
        });
    }


    @Override
    public void showData(List<DealerShippingItemBean> dataBean) {
        if (dealerShippingListRefresh.isRefreshing()){
            dealerShippingListRefresh.setRefreshing(false);
        }
        if (dataBean!=null) {
            dealerShippingAdapter.setNewData(dataBean);
        }
    }

    @Override
    public void showFailure(String msg) {
        ToastUtil.showToast(msg);
        if (dealerShippingListRefresh.isRefreshing()){
            dealerShippingListRefresh.setRefreshing(false);
        }
    }

    @Override
    public void defaultResult(boolean isSuccess) {

    }

    @Override
    public void showSelectResult(boolean isSuccess) {
        if (isSuccess){
            DealerShippingItemBean bean = dealerShippingAdapter.getData().get(clickPos);

            if (!TextUtils.isEmpty(dealerShippingAdapter.getSelectId())) {
                for (DealerShippingItemBean itemBean : dealerShippingAdapter.getData()) {
                    if (itemBean.getDelivery_id().equals(dealerShippingAdapter.getSelectId())){
                        itemBean.setCheck_delivery("0");
                        break;
                    }
                }
            }
            bean.setCheck_delivery("1");
            dealerShippingAdapter.notifyDataSetChanged();

            Intent intent = new Intent();
            intent.putExtra("delivery_id",bean.getDelivery_id());
            intent.putExtra("delivery_name",bean.getTitle());
            setResult(RESULT_OK,intent);
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x87&&resultCode == RESULT_OK){
            refreshData();
        }
    }
}
